package com.gistone.commis.spring;


import com.gistone.commis.api.restController.SWageController;
import com.gistone.commis.entity.AttendanceNotify;
import com.gistone.commis.entity.Community;
import com.gistone.commis.entity.OvertimeNotify;
import com.gistone.commis.mapper.AttendanceNotifyMapper;
import com.gistone.commis.mapper.CommunityMapper;
import com.gistone.commis.mapper.OvertimeNotifyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTasks {
    private static final Logger logger = LoggerFactory.getLogger(SWageController.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private OvertimeNotifyMapper overtimeNotifyMapper;


    @Autowired
    private AttendanceNotifyMapper attendanceNotifyMapper;

    @Autowired
    private CommunityMapper communityMapper;
    /*@Scheduled(cron="0/5 * *  * * ? ")   //5秒执行
     */

   // 0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务
//    @Scheduled(cron="0 0 2 * * ? ")   //每天早上2点执行

    @Scheduled(cron="0 0 2 20 * ? ")//表示在每月的20日的凌晨2点调度任务
    public void generateNotify() {
        logger.info("Starting scheduled task，The time is now " + dateFormat.format(new Date()));
        Calendar now = Calendar.getInstance();
        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH) + 1;
        List<Community> communityList = communityMapper.selectCommunitysWithout1();

        //先判定每个社区的考勤数据是否上传
        for (Community community : communityList) {

            int commmunityId = community.getCommunityId();

            if (attendanceNotifyMapper.selectByCommunityAndYearAndMonthAndFlag(commmunityId, nowYear, nowMonth, 0).size() == 1) {

                logger.info("attendance has generate");
            } else if (attendanceNotifyMapper.selectByCommunityAndYearAndMonthAndFlag(commmunityId, nowYear, nowMonth, 0).size() == 0) {

                AttendanceNotify attendanceNotify = new AttendanceNotify();
                attendanceNotify.setCommunityId(commmunityId);
                attendanceNotify.setYear(nowYear);
                attendanceNotify.setMonth(nowMonth);
                attendanceNotify.setFlag(0);
                attendanceNotifyMapper.insertSelective(attendanceNotify);
                logger.info("add attendance notify--" + attendanceNotify.toString());


            }

            if (overtimeNotifyMapper.selectByCommunityAndYearAndMonthAndFlag(commmunityId, nowYear, nowMonth, 0).size() == 1) {

                logger.info("overtime has  generate");
            } else if (overtimeNotifyMapper.selectByCommunityAndYearAndMonthAndFlag(commmunityId, nowYear, nowMonth, 0).size() == 0) {

                OvertimeNotify overtimeNotify = new OvertimeNotify();
                overtimeNotify.setCommunityId(commmunityId);
                overtimeNotify.setYear(nowYear);
                overtimeNotify.setMonth(nowMonth);
                overtimeNotify.setFlag(0);
                overtimeNotifyMapper.insertSelective(overtimeNotify);
                logger.info("add overtime notify--" + overtimeNotify.toString());


            }




        }

        logger.info("end scheduled task，The time is now " + dateFormat.format(new Date()));


    }
}